﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NHibernateDB;

namespace MedicalDB
{
    public partial class VizualizarePacienti : UserControl
    {

        FormDBMedical main;
        string limba;
        int _previouslySortedColumn = -1;

        public VizualizarePacienti(string limba, FormDBMedical main)
        {
            InitializeComponent();
            Rename(limba);
            this.limba = limba;

            listaPacienti.View = View.Details;
            listaPacienti.FullRowSelect = true;
            listaPacienti.GridLines = false;

            string xmlFile = @"C:\Language" + limba + ".xml";
            XMLParser xmlParser = new XMLParser(xmlFile);
            List<string> namesForm = xmlParser.ExtractAttribute(listaPacienti.Name.ToString(), string.Empty, string.Empty, "name");

            foreach(string s in namesForm)
                listaPacienti.Columns.Add(s);
          

            PersistenceManager pm = PersistenceManager.Instance;
            IList<DateGeneralePacient> lista = pm.RetrieveAll<DateGeneralePacient>(SessionAction.BeginAndEnd);
            listaPacienti.Items.Clear();
            populateListView(lista);    

            this.main = main;
        }

        void listview1_ColumnClick(object sender, System.Windows.Forms.ColumnClickEventArgs e)
        {

            ListViewComparer lvComparer;
            if (null == listaPacienti.ListViewItemSorter)
            {
                lvComparer = new ListViewComparer();
            }
            else
            {
                lvComparer = (ListViewComparer)listaPacienti.ListViewItemSorter;
            }
            if (e.Column != _previouslySortedColumn)
            {
                listaPacienti.Sorting = SortOrder.None;
            }
            if (listaPacienti.Sorting == SortOrder.Ascending)
            {
                listaPacienti.Sorting = SortOrder.Descending;
            }
            else
            {
                listaPacienti.Sorting = SortOrder.Ascending;
            }
            LVSort.SetSortIcons(listaPacienti, ref _previouslySortedColumn, e.Column);
            lvComparer.SortColumn = e.Column;
            listaPacienti.ListViewItemSorter = (System.Collections.IComparer)lvComparer; //this immediately causes a sort to happen

            foreach (ColumnHeader ch in this.listaPacienti.Columns)
            {
                ch.Width = -2;
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (!tbCNP.Text.Equals(string.Empty))
            {
                PersistenceManager pm = PersistenceManager.Instance;
                IList<DateGeneralePacient> lista2 = pm.RetrieveEquals<DateGeneralePacient>("IdPacient", tbCNP.Text);
                listaPacienti.Items.Clear();
                populateListView(lista2);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            PersistenceManager pm = PersistenceManager.Instance;
            IList<DateGeneralePacient> lista = pm.RetrieveAll<DateGeneralePacient>(SessionAction.BeginAndEnd);
            listaPacienti.Items.Clear();
            populateListView(lista);    
        }

        private void populateListView(IList<DateGeneralePacient> list)
        {
            foreach (DateGeneralePacient dgp in list)
            {
                ListViewItem item = new ListViewItem(dgp.IdPacient);
                item.SubItems.Add(dgp.Nume);
                item.SubItems.Add(dgp.Prenume);
                item.SubItems.Add(dgp.Sex);
                item.SubItems.Add(dgp.DataNasterii.ToString());
                item.SubItems.Add(dgp.LoculNasterii);
                item.SubItems.Add(dgp.Resedinta);
                item.SubItems.Add(dgp.DomiciliuLegal);
                item.SubItems.Add(dgp.GrupaSanguina);
                item.SubItems.Add(dgp.Ocupatie);
                item.SubItems.Add(dgp.LocMunca);
                item.SubItems.Add(dgp.TalonPensie);
                listaPacienti.Items.Add(item);
            }

            foreach (ColumnHeader ch in this.listaPacienti.Columns)
            {
                ch.Width = -2;
            }

        }

        private void btSelectarePacient_Click(object sender, EventArgs e)
        {
           
            for (int i = 0; i < listaPacienti.Items.Count; i++)
            {
                if (listaPacienti.Items[i].Selected == true)
                {
                    ListViewItem itm = listaPacienti.SelectedItems[0];
                    FormDBMedical.CNP = itm.SubItems[0].Text;
                    FormDBMedical.nume = itm.SubItems[1].Text;
                    FormDBMedical.prenume = itm.SubItems[2].Text;
                    FormDBMedical.ActualizarePacientNume();
                    break;
                }       
            }
        }

        private void ListaPacienti_DoubleClick(object sender, EventArgs e)
        {
            for (int i = 0; i < listaPacienti.Items.Count; i++)
            {
                if (listaPacienti.Items[i].Selected == true)
                {
                    main.resetGUI();
                    ListViewItem itm = listaPacienti.SelectedItems[0];

                    FormDBMedical.CNP = itm.SubItems[0].Text;
                    FormDBMedical.nume = itm.SubItems[1].Text;
                    FormDBMedical.prenume = itm.SubItems[2].Text;
                    FormDBMedical.ActualizarePacientNume();

                    VizualizareDate vizualizareDate = new VizualizareDate(itm.SubItems[0].Text,limba);
                    vizualizareDate.Padding = new System.Windows.Forms.Padding(3, 50, 3, 3);
                    vizualizareDate.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                                | System.Windows.Forms.AnchorStyles.Left)
                                | System.Windows.Forms.AnchorStyles.Right)));
                    vizualizareDate.Dock = DockStyle.Fill;
                    main.control = vizualizareDate;
                    main.Controls.Add(vizualizareDate);
                }

            }
        }

        private void btVizualizarePacienti_Click(object sender, EventArgs e)
        {
            PersistenceManager pm = PersistenceManager.Instance;
            IList<DateGeneralePacient> lista = pm.RetrieveAll<DateGeneralePacient>(SessionAction.BeginAndEnd);
            listaPacienti.Items.Clear();
            populateListView(lista);
        }
    }

}
